from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .config import settings
from .routers import upload
import logging

logger = logging.getLogger(__name__)


def create_app() -> FastAPI:
    """
    创建并配置FastAPI应用
    """
    # 创建FastAPI实例
    app = FastAPI(
        title=settings.PROJECT_NAME,
        version=settings.VERSION
    )

    # 配置CORS
    app.add_middleware(
        CORSMiddleware,
        allow_origins=settings.CORS_ORIGINS,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # 注册路由
    app.include_router(upload.router)

    @app.on_event("startup")
    async def startup_event():
        logger.info("应用程序启动...")

    @app.get("/health")
    async def health_check():
        return {"status": "healthy"}

    return app